<?php
namespace app\index\controller;
use think\Controller;
use think\Db;
use think\Request;

class PersonalPet extends IndexController
{
	public function index()
	{

        $pet = Db::name('pet')->where('use_id',input('session.user.id'))->find();
        if($pet){
        	$this->assign('list', $pet);
        }


        return view('personal_pet/index');
	}



	public function edit()
    {

    	$result = db('pet')->where('use_id',input('session.user.id'))->select();
    	if(!$result){
    		return $this->error('您还没有添加过宠物');
    	}

        if (request()->isPost()) {
            $data = [
                'use_id' => input('use_id'),
                'nickname' => input('nickname'),
                'sex' => input('sex'),
                'birthday' => input('birthday'),
                'category' => input('category'),
                'weight' => input('weight'),
                'xuetong' => input('xuetong'),
                'status' => input('status'),

            ];

            // var_dump($data);exit;
            $validate = validate('Pet');
            if ($validate->check($data)) {
                if (Db::name('pet')->where('use_id',$data['use_id'])->update(['nickname'=>$data['nickname'],'sex'=>$data['sex'],'birthday'=>$data['birthday'],'category'=>$data['category'],'weight'=>$data['weight'],'xuetong'=>$data['xuetong'],'status'=>$data['status']])) {

                    //动态
                    $dongtai = ['use_id' => input('session.user.id'),'dtime' => date("Y-m-d H:i:s"),'caozuo' => '更新了宠物信息'];
                    db('dongtai')->insert($dongtai);

                    return $this->success('保存信息成功');
                } else {
                    return $this->error('保存信息失败');
                }
            } else {
                return $this->error($validate->getError());
            }
            return;
        }
        return $this->fetch();
    }




    public function addpet()
    {
    	return view('personal_pet/add');
    }

    public function doaddpet()
    {	

    	$result = db('pet')->where('use_id',input('session.user.id'))->select();
    	if($result){
    		return $this->error('您已经添加过宠物了');
    	}

    	if (request()->isPost()) {
            $data = [
                'use_id' => input('use_id'),
                'nickname' => input('nickname'),
                'sex' => input('sex'),
                'birthday' => input('birthday'),
                'category' => input('category'),
                'weight' => input('weight'),
                'xuetong' => input('xuetong'),
                'status' => input('status'),

            ];

            $validate = validate('Pet');
            if ($validate->check($data)) {
                if(db('pet')->insert($data)){

                    //动态
                    $dongtai = ['use_id' => input('session.user.id'),'dtime' => date("Y-m-d H:i:s"),'caozuo' => '添加了新宠物'.$data['nickname']];
                    db('dongtai')->insert($dongtai);

                    return $this->success('保存信息成功');
                }else{
                    return $this->error('保存信息失败');
                }

            }
            return json($validate->getError());
 
        }
        return json('what do you want to do?');
    }



    public function petphoto()
    {
        $pet = Db::name('pet')->where('use_id',input('session.user.id'))->select();
        if($pet){
            $this->assign('list', $pet[0]);
        }
        

        return view('personal_pet/photo');

    }


    public function uploadPetPhoto()
    {

        $file = request()->file('image');
        if(!$file){
            return $this->error('头像上传失败');
        }


        $info = $file->validate(['size'=>100000000,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads');
        if($info){
            //上传成功
            if(Db::name('pet')->where('use_id',input('session.user.id'))->update(['icon'=>$info->getSaveName()])){
                return $this->success('头像上传成功');
            }else{
                return $this->error('头像上传失败');
            }
        }else{
            return $this->error($file->getError());
        }
    }



}